﻿using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using AppFramework.DinosaursParkMes.Process.DTO;
using AppFramework.DinosaursParkMes.WorkOrder;

namespace AppFramework.DinosaursParkMes.Process
{
    public class CraftRouteAppService : AppFrameworkAppServiceBase, ICraftRouteAppService
    {
        private readonly IRepository<CraftRouteModel> repository;
        private readonly IRepository<CraftProcessModel> repository1;
        private readonly IWorkOrderAppService workOrderApp;
        public CraftRouteAppService(IRepository<CraftRouteModel> repository = null, IRepository<CraftProcessModel> repository1 = null, IWorkOrderAppService workOrderApp = null)
        {
            this.repository = repository;
            this.repository1 = repository1;
            this.workOrderApp = workOrderApp;
        }
        /// <summary>
        /// 工艺路线添加
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("CraftAdd")]
        public async Task<int> PostCraftAdd([FromBody]CraftRouteAddDTO dto)
        {
            //CraftRouteModel craftRoute = new CraftRouteModel(dto.Routecoded, dto.Routename, dto.Routestate, dto.Routeexplain, dto.Routeremark);
            //await repository.InsertAsync(craftRoute);
            //abp框架对工作单元进行了封装
            //开始事务
            var x = UnitOfWorkManager.Begin();
            try
            {
                var dtos = ObjectMapper.Map<CraftRouteModel>(dto);
                var modelname = "CraftRoute";
                dtos.Routecoded = await workOrderApp.GetCode(modelname);
                await repository.InsertAsync(dtos);
                foreach (var item in dto.Item)
                {
                    var dtos2 = ObjectMapper.Map<CraftProcessModel>(item);
                    dtos2.Craftcode = dtos.Routecoded;
                    await repository1.InsertAsync(dtos2);
                }
                //提交事务
                x.Complete();
                return 1;
            }
            catch (Exception)
            {
                //回滚事务
                x.Dispose();
                throw;
            }
        }
        /// <summary>
        /// 工艺路线显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("GetCraftShow")]
        public async Task<PagedResultDto<CraftRouteDTO>> GetCraftShow(CraftRouteChaDTO dto)
        {
            //var list = repository.GetAll();

            //var craftshow = list.OrderBy(craftRoute.Sorting ?? "id asc").PageBy(craftRoute);

            //var nocount = await list.CountAsync();
            //var dblist = await craftshow.ToListAsync();
            //var retures = ObjectMapper.Map<List<CraftRouteDTO>>(dblist);

            //return new PagedResultDto<CraftRouteDTO>(nocount, retures);
            PagedResultDto<CraftRouteDTO> dtos = new PagedResultDto<CraftRouteDTO>();
            var list = from x in repository.GetAll() where x.IsDeleted == false
                       select new CraftRouteDTO
                       {
                           Id = x.Id,
                           Routename = x.Routename,
                           Routecoded = x.Routecoded,
                           Routestate = x.Routestate,
                           Routeexplain = x.Routeexplain,
                           Routeremark = x.Routeremark,
                       };
            if(!string.IsNullOrEmpty(dto.RoutecodedName))
            {
                list = list.Where(m => m.Routecoded==dto.RoutecodedName || m.Routename==dto.RoutecodedName);
            }
            if(!string.IsNullOrEmpty(dto.Routestate))
            {
                list = list.Where(m => m.Routestate == dto.Routestate);
            }

            dtos.TotalCount = list.Count();
            dtos.Items = await list.OrderByDescending(p => p.Id).Skip((dto.pageindex - 1) * dto.pagesize).Take(dto.pagesize).ToListAsync();
            return dtos;
        }

      

    }
}
